package org.ethan.user.service.impl;

import org.ethan.basic.exception.BasicException;
import org.ethan.basic.service.impl.BaseServiceImpl;
import org.ethan.basic.util.MD5Utils;
import org.ethan.basic.util.StrUtils;
import org.ethan.user.constant.UserConst;
import org.ethan.user.domain.LoginInfo;
import org.ethan.user.domain.User;
import org.ethan.user.dto.UserDto;
import org.ethan.user.mapper.LoginInfoMapper;
import org.ethan.user.mapper.UserMapper;
import org.ethan.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 手机号注册业务
     * @param userDto
     */
    @Override
    public void phoneRegister(UserDto userDto) {
        // 1.校验输入的四个字段是否为空
        if (StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPwd())
                || StringUtils.isEmpty(userDto.getVerifyCode())) {
            throw new BasicException("不能为空！！！");
        }

        // 2.校验手机号是否注册
        User phone = userMapper.findByPhone(userDto.getPhone());
        if (phone != null) {
            throw new BasicException("用户已存在！！！");
        }

        // 3.校验两次输入的密码是否一致
        if (!userDto.getPassword().equals(userDto.getConfirmPwd())) {
            throw new BasicException("密码不一致，请检查！！!");
        }

        // 4.校验验证码是否正确
        Object verifyCode = redisTemplate.opsForValue().get(UserConst.REGISTER_PREFIX + userDto.getPhone());
        if (verifyCode == null) {
            throw new BasicException("验证码已过期，请重新获取！！！");
        } else {
            String code = verifyCode.toString().split(":")[1];
            if (!userDto.getVerifyCode().equalsIgnoreCase(code)) {
                throw new BasicException("验证码错误！！！");
            }
        }

        // 校验成功，就把输入的数据保存到数据库表
        // 必须先保存到logininfo表，再保存到user表，因为要先获得user表的logininfo_id字段
        // 准备user数据和logininfo数据
        User user = userDto2User(userDto);
        LoginInfo loginInfo = user2LoginInfo(user);

        loginInfoMapper.add(loginInfo); // 返回自增的id，从而让user表获得logininfo_id

        user.setLogininfo_id(loginInfo.getId());

        userMapper.add(user);
    }


    /**
     * 获取LoginInfo对象
     * @param user
     * @return
     */
    private LoginInfo user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();

        // copy user中的对应数据到logininfo中
        BeanUtils.copyProperties(user, loginInfo);
        // type 1普通用户，0管理员
        loginInfo.setType(1);
        // disable 1启用
        loginInfo.setDisable(1);

        return loginInfo;

    }


    /**
     * 获取User对象
     * @param userDto
     * @return
     */
    private User userDto2User(UserDto userDto) {
        User user = new User();

        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());

        user.setState(1);
        // 设置盐值‘
        String salt = StrUtils.getComplexRandomString(32);
        // 设置加密密码，先加密再加盐
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);

        user.setSalt(salt);
        user.setPassword(password);

        return user;

    }
}
